<template>
  <Panel>
    <template
      >商品分类
      <!-- 按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" size="mini" @click="dialog"
            >添加分类</el-button
          >
        </el-col>
      </el-row>

      <!-- biaoge -->
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="80">
          </el-table-column>
          <!--  prop="cateName" -->
          <el-table-column label="分类名称" width="180">
            <template v-slot="{ row }">
              <el-input v-if="row.flag" v-model="row.cateName"></el-input>
              <span v-else>{{ row.cateName }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="state" label="是否启用">
            <template v-slot="{ row }">
              <el-switch
                v-model="row.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                :disabled="!row.flag"
              >
                <!--disabled本身是禁用,加上row.flag是随着true和false来选择禁用  -->
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template v-slot="{ row }">
              <el-button
                :type="row.flag ? 'default' : 'primary'"
                size="mini"
                @click="fn(row)"
                >{{ row.flag ? '完成' : '编辑' }}</el-button
              >
              <el-button type="danger" size="mini" @click="delItem(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>

      <!-- 添加分类后的弹出框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <!-- <span>这是一段信息</span>  :rules="rules"-->
        <template>
          <el-form
            :model="ruleForm"
            ref="cateRef"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="分类名称" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="是否启用" prop="delivery">
              <el-switch v-model="ruleForm.delivery"></el-switch>
            </el-form-item>
          </el-form>
        </template>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分液 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </template>
  </Panel>
</template>

<script>
import { getpaging, addCates, delCates, setCates } from '../../api/good.js'
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 2,
      total: 0,
      dialogVisible: false,
      ruleForm: {
        name: '',
        delivery: true
      }
    }
  },
  created() {
    this.getDatas()
  },
  methods: {
    dialog() {
      this.dialogVisible = true
    },
    async getDatas() {
      const { data, total } = await getpaging({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      console.log(data)
      // 请求回来的数据添加自定义的事件//因为只有点了编辑才能开启开关
      data.forEach(item => {
        item.flag = false
      })

      this.tableData = data
      this.total = total
    },
    handleSizeChange(val) {
      /* console.log(`每页 ${val} 条`) */
      this.pageSize = val
      this.getDatas()
    },
    handleCurrentChange(val) {
      /* console.log(`当前页: ${val}`) */
      this.currentPage = val
      this.getDatas()
    },
    handleClose() {
      console.log('aa')
      this.$refs.cateRef.resetFields()
      this.dialogVisible = false
      /*  this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {}) */
    },
    async addCate() {
      this.dialogVisible = true
      const { code, msg } = await addCates({
        cateName: this.ruleForm.name,
        state: this.ruleForm.delivery
      })
      // console.log(data)
      if (code === 0) {
        this.$message({
          type: 'success',
          massage: msg
        })
      }
      this.handleClose()
      this.dialogVisible = false
    },
    /* 删除用户 */
    delItem(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code, msg } = await delCates({ id })
          if (code === 0) {
            this.$message({
              type: 'success',
              message: msg
            })
            this.getDatas()
          }
        })

        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    /* 最后一个方法 */
    async fn(row) {
      if (row.flag) {
        if (row.cateName === '') return
        // 要提交请求了
        const { code, msg } = await setCates({
          id: row.id,
          cateName: row.cateName,
          state: Boolean(row.state)
        })
        if (code === 0) {
          this.$message({
            type: 'success',
            message: msg
          })
        }
        /* console.log(data) */
        row.flag = false
      } else {
        // 编辑按钮
        row.flag = true
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
